#include "voltagemeteritem.h"
#include <QPainter>

VoltageMeter::VoltageMeter(double value, QGraphicsItem *parent)
    : CircuitComponent(parent)
    , m_value(value)
{

}

QPointF VoltageMeter::inputPort() const
{
    return QPointF(-25, 0); // 本地坐标系
}

QPointF VoltageMeter::outputPort() const
{
    return QPointF(40, 0);
}

void VoltageMeter::setValue(double value)
{
    m_value = value;
    update();
}

void VoltageMeter::setText(const QString &text)
{
    m_text = text;
}

void VoltageMeter::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    painter->setRenderHint(QPainter::Antialiasing);
    painter->setPen(QColor(122,175,227,255));
    painter->drawEllipse(-20, -20, 55, 55);

    // 绘制端口
    painter->drawRect(-30, 0, 10, 10); // 输入端口
    painter->drawRect(35, 0, 10, 10);   // 输出端口

    painter->setPen(QPen(Qt::white));
    //绘制文本信息
    painter->drawText(QRect(-40, -40, 120, 20), Qt::AlignLeft,m_text);

    painter->setPen(Qt::white);
    painter->drawText(QRect(-20, -20, 55, 55), Qt::AlignCenter,
                    QString::number(m_value, 'f', 1) + "V");
}
